// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.PurchaseRgoodsOrder;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderImportPo;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderItemVo;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderSearchParamPo;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderVo;
import com.fowo.api.service.PurchaseRgoodsOrderService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "采购退货单")
@RequestMapping("/api/purchase-rgoods-order")
@RestController
public class PurchaseRgoodsOrderController {

  @Resource
  private PurchaseRgoodsOrderService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<PurchaseRgoodsOrderItemVo>> search(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<PurchaseRgoodsOrderItemVo>>> searchOptions(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(PurchaseRgoodsOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<PurchaseRgoodsOrderItemVo>> getPage(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(PurchaseRgoodsOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<PurchaseRgoodsOrderVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取备注相关的编号列表")
  @GetMapping("remark/{texts}/ids")
  public R<List<Long>> getIdsByRemark(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByRemark(nameList));
  }

  @Operation(summary = "获取编号相关的备注列表")
  @GetMapping("remark/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getRemarkByIds(idList));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody PurchaseRgoodsOrderVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody PurchaseRgoodsOrderVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody PurchaseRgoodsOrderVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<PurchaseRgoodsOrderImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<PurchaseRgoodsOrderImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "采购退货单导入模版.xlsx");
    InputStream inputStream = new ClassPathResource(
      "templates/PurchaseRgoodsOrder.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    PurchaseRgoodsOrderSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderWaste')")
  @Operation(summary = "作废")
  @PostMapping("waste")
  public R waste(@RequestBody IdListPo po) {
    return R.run(() -> service.waste(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderGenerateReceiptOrder')")
  @Operation(summary = "生成收款单")
  @PostMapping("generateReceiptOrder")
  public R generateReceiptOrder(@RequestBody IdPo po) {
    return R.run(() -> service.generateReceiptOrder(po.getId()));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderWasteFrom')")
  @Operation(summary = "作废")
  @PostMapping("wasteFrom")
  public R wasteFrom(@RequestBody IdPo po) {
    return R.run(() -> service.wasteFrom(po.getId()));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderAddReturnOrder')")
  @Operation(summary = "添加退货单")
  @PostMapping("addReturnOrder")
  public R addReturnOrder(@RequestBody IdListPo po) {
    return R.run(() -> service.addReturnOrder(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('purchaseRgoodsOrderBatchApproval')")
  @Operation(summary = "批量审核")
  @PostMapping("batchApproval")
  public R batchApproval(@RequestBody IdListPo po) {
    return R.run(() -> service.batchApproval(po.getIds()));
  }

  @Operation(summary = "全部")
  @GetMapping("all-retrun-order")
  public R<List<PurchaseRgoodsOrderItemVo>> allRetrunOrder(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allRetrunOrder(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-retrun-orderPage")
  public R<Page<PurchaseRgoodsOrderItemVo>> allRetrunOrderPage(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allRetrunOrderPage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("all-retrun-orderPage-with-child")
  public R<Page<?>> allRetrunOrderPageWithChild(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.allRetrunOrderPageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-retrun-orderLastUpdateTime")
  public R<Date> allRetrunOrderLastUpdateTime(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allRetrunOrderLastUpdateTime(search));
  }

  @Operation(summary = "待审批")
  @GetMapping("pending-approval")
  public R<List<PurchaseRgoodsOrderItemVo>> pendingApproval(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingApproval(search));
  }

  @Operation(summary = "待审批分页")
  @GetMapping("pending-approvalPage")
  public R<Page<PurchaseRgoodsOrderItemVo>> pendingApprovalPage(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingApprovalPage(search));
  }

  @Operation(summary = "待审批包括子表的查询结果")
  @GetMapping("pending-approvalPage-with-child")
  public R<Page<?>> pendingApprovalPageWithChild(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pendingApprovalPageWithChildSearch(search));
  }

  @Operation(summary = "待审批最后更新时间")
  @GetMapping("pending-approvalLastUpdateTime")
  public R<Date> pendingApprovalLastUpdateTime(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingApprovalLastUpdateTime(search));
  }

  @Operation(summary = "已处理")
  @GetMapping("order-processed")
  public R<List<PurchaseRgoodsOrderItemVo>> orderProcessed(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderProcessed(search));
  }

  @Operation(summary = "已处理分页")
  @GetMapping("order-processedPage")
  public R<Page<PurchaseRgoodsOrderItemVo>> orderProcessedPage(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderProcessedPage(search));
  }

  @Operation(summary = "已处理包括子表的查询结果")
  @GetMapping("order-processedPage-with-child")
  public R<Page<?>> orderProcessedPageWithChild(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.orderProcessedPageWithChildSearch(search));
  }

  @Operation(summary = "已处理最后更新时间")
  @GetMapping("order-processedLastUpdateTime")
  public R<Date> orderProcessedLastUpdateTime(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderProcessedLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("order-voided")
  public R<List<PurchaseRgoodsOrderItemVo>> orderVoided(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderVoided(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("order-voidedPage")
  public R<Page<PurchaseRgoodsOrderItemVo>> orderVoidedPage(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderVoidedPage(search));
  }

  @Operation(summary = "已作废包括子表的查询结果")
  @GetMapping("order-voidedPage-with-child")
  public R<Page<?>> orderVoidedPageWithChild(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.orderVoidedPageWithChildSearch(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("order-voidedLastUpdateTime")
  public R<Date> orderVoidedLastUpdateTime(
    PurchaseRgoodsOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.orderVoidedLastUpdateTime(search));
  }
}
